<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 商品分类卡片视图 -->
    <el-card>
      <el-button type="primary" @click = "showaddCatadialog">添加分类</el-button>
      <tree-table class="tree_table" :data="catalogList"  :columns="columns" :selection-type="false" :expand-type="false" show-index index-text="#" border>
      <template slot="isok" slot-scope="scope">
        <i v-show="!scope.row.cat_deleted"  class="el-icon-success" style="font-size: 20px; color: green" ></i>
        <i v-show="scope.row.cat_deleted" class="el-icon-error" style="font-size: 20px; color: red"></i>
      </template >
      <template slot= "order" slot-scope="scope">
        <el-tag v-if="scope.row.cat_level === 0">第一级</el-tag>
        <el-tag v-if="scope.row.cat_level === 1" type="success">第二级</el-tag>
        <el-tag v-if="scope.row.cat_level === 2" type="warning">第三级</el-tag>
      </template>
      <template slot="opt" >
        <el-button type="primary" size="mini" icon="el-icon-edit" >编辑</el-button>
        <el-button type="danger" size="mini" icon="el-icon-delete"> 删除</el-button>
      </template>
      </tree-table>
      <!-- 分页页码 -->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        layout="total, prev, pager, next, jumper"
        :total="pagetotal">
      </el-pagination>
    </el-card>
    <!-- 添加分类弹框 -->
    <el-dialog title="添加分类" :visible.sync="addCatadialogVisible" width="50%" @close="canceldialog">
      <el-form ref="addCataformRef" :rules="formrules" :model="addCataform" label-width = "90px">
        <el-form-item label="分类名称:"  prop ="cat_name">
          <el-input v-model="addCataform.cat_name" ></el-input>
        </el-form-item>
        <el-form-item label="父级分类:" >
          <el-cascader clearable v-model="selectPCataId" :options="parentList" :props="CataProps" @change="ParentCatalogChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="canceldialog">取 消</el-button>
        <el-button type="primary" @click="addCata">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created () {
    this.getCatalogList()
  },
  data () {
    return {
      addCatadialogVisible: false,
      pagetotal: 0,
      // 商品分类数据列表
      catalogList: [],
      // 请求分类列表参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
          minWidth: '100px'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          template: 'isok',
          type: 'template'
        },
        {
          label: '排序',
          prop: 'cat_level',
          template: 'order',
          type: 'template'
        },
        {
          label: '操作',
          template: 'opt',
          type: 'template'
        }
      ],
      // 添加分类表单验证规则
      formrules: {
        cat_name: [{ required: true, message: '请输入分类名', trigger: 'blur' }]
      },
      addCataform: {
        // 将要添加的分类名
        cat_name: '',
        // 级联表选中父id
        select_p_Id: 0,
        // 分类层级
        cat_level: 0
      },
      parentList: [],
      // 级联表选中父id数组
      selectPCataId: [],
      // 级联属性
      CataProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true
      }
    }
  },
  methods: {
    // 获取分类列表
    async getCatalogList () {
      const { data: res } = await this.$http.get('/categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.pagetotal = res.data.total
      this.catalogList = res.data.result
      console.log(res)
    },
    handleCurrentChange (val) {
      // console.log('第' + val + '页')
      this.queryInfo.pagenum = val
      this.getCatalogList()
    },
    ParentCatalogChange () {
      if (this.selectPCataId.length > 0) {
        this.addCataform.select_p_Id = this.selectPCataId[this.selectPCataId.length - 1]
        this.addCataform.cat_level = this.selectPCataId.length
        return
      } else {
        this.addCataform.select_p_Id = 0
        this.addCataform.cat_level = 0
      }
      console.log(this.addCataform)
      console.log(this.selectPCataId)
    },
    // 获取父级分类节点
    async getParentCatalog () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) this.$message.error(res.meta.msg)
      this.parentList = res.data
      // console.log(this.parentList)
    },
    showaddCatadialog () {
      this.getParentCatalog()
      this.addCatadialogVisible = true
    },
    addCata () {
      // console.log(this.addCataform)
      this.$refs.addCataformRef.validate(async vaild => {
        if (!vaild) return
        const { data: res } = await this.$http.post('categories', this.addCataform)
        // console.log('add')
        console.log(this.addCataform)
        console.log(this.selectPCataId)
        console.log(res)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.addCatadialogVisible = false
        this.getCatalogList()
      })
    },
    // 取消添加
    canceldialog () {
      this.$refs.addCataformRef.resetFields()
      this.selectPCataId = []
      this.addCataform.cat_level = 0
      this.addCataform.select_p_Id = 0
      this.addCatadialogVisible = false
    }
  }
}
</script>
<style lang="less" scoped>
.tree_table{
  margin-top: 20px;
}
.el-cascader{
  width: 100%;
}
</style>
